package com.fwtai.controller.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.service.core.UserService;
import com.fwtai.service.web.PartnerService;
import com.fwtai.tool.ToolClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 合伙人控制层|路由层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-08-02 15:26:31
 * @官网 <url>http://www.fwtai.com</url>
 */
@RestController
@RequestMapping("/partner")
public class PartnerController{

  private final PartnerService partnerService;

  private final UserService userService;

  public PartnerController(final PartnerService partnerService,final UserService userService){
    this.partnerService = partnerService;
    this.userService = userService;
  }

  /**审核*/
  @PreAuthorize("hasAuthority('partner_row_edit')")
  @PostMapping("/edit")
  public void edit(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(partnerService.edit(request),response);
  }

  //获取邀请人列表
  @PreAuthorize("hasAuthority('partner_row_inviter')")
  @GetMapping("/getUserInviter")
  public void getUserInviter(final String word,final HttpServletResponse response){
    ToolClient.responseJson(partnerService.getUserType(word),response);
  }

  //获取推荐人列表
  @PreAuthorize("hasAuthority('partner_row_referrer')")
  @GetMapping("/getUserReferrer")
  public void getUserReferrer(final String word,final HttpServletResponse response){
    ToolClient.responseJson(partnerService.getUserType(word),response);
  }

  //保存邀请人
  @PreAuthorize("hasAuthority('partner_row_add_inviter')")
  @PostMapping("/addUserInviter")
  public void addUserInviter(final Long inviter,final Long userId,final HttpServletResponse response){
    ToolClient.responseJson(partnerService.addUserType(null,inviter,userId,1),response);
  }

  //保存推荐人
  @PreAuthorize("hasAuthority('partner_row_add_referrer')")
  @PostMapping("/addUserReferrer")
  public void addUserReferrer(final Long kid,final Long inviter,final Long userId,final HttpServletResponse response){
    ToolClient.responseJson(partnerService.addUserType(kid,inviter,userId,2),response);//inviter要修改的代理人的userId
  }

  /**根据id查询对应的数据*/
  @PreAuthorize("hasAuthority('partner_row_queryById')")
  @GetMapping("/queryById")
  public void queryById(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(partnerService.queryById(new PageFormData(request)),response);
  }

  /**获取数据*/
  @PreAuthorize("hasAuthority('partner_btn_listData')")
  @GetMapping("/listData")
  public void listData(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(partnerService.listData(new PageFormData(request)),response);
  }

  /**获取角色数据*/
  @PreAuthorize("hasAuthority('partner_btn_row_getAllotRole')")
  @GetMapping("/getAllotRole")
  public void getAllotRole(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(userService.getAllotRole(ToolClient.getFormData(request)),response);
  }

  /**保存分配角色*/
  @PreAuthorize("hasAuthority('partner_btn_row_saveAllotRole')")
  @PostMapping("/saveAllotRole")
  public void saveAllotRole(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(userService.saveAllotRole(ToolClient.getFormData(request)),response);
  }

  /**获取菜单用于分配私有菜单*/
  @PreAuthorize("hasAuthority('partner_row_getOwnMenu')")
  @GetMapping("/getOwnMenu")
  public void getOwnMenu(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(userService.getOwnMenu(ToolClient.getFormData(request)).replaceAll("\"false\"","false").replaceAll("\"true\"","true"),response);
  }

  /**保存私有菜单*/
  @PreAuthorize("hasAuthority('partner_row_saveOwnMenu')")
  @PostMapping("/saveOwnMenu")
  public void saveOwnMenu(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(userService.saveOwnMenu(ToolClient.getFormData(request)),response);
  }

  /**签约|解约操作*/
  @PreAuthorize("hasAuthority('partner_row_protocol')")
  @PostMapping("/editProtocol")
  public void editProtocol(final Long kid,final Integer target,final HttpServletResponse response){
    ToolClient.responseJson(partnerService.editProtocol(kid,target),response);
  }
}